<template>
    <div v-show="titleShow" v-if="authLoaded" class="title-wrapper">
        <slot></slot>
        <div class="ui small form  grid">
            <div class="search eight wide column">
                <button title="上线" v-if="btns.onOff.enable"
                        class="ui circular icon primary button tiny"
                        @click.stop.prevent="batch('onOff',1)">
                    <i class="icon-enable"></i>
                </button>
                <button title="下线" v-if="btns.onOff.enable"
                        class="ui circular icon primary button tiny"
                        @click.stop.prevent="batch('onOff',0)">
                    <i class="icon-disable"></i>
                </button>
                <button title="删除" v-if="btns.onOff.enable&&need_del"
                        class="ui circular icon primary button tiny"
                        @click.stop.prevent="batch('onOff',-1)">
                    <i class="icon remove"></i>
                </button>
                <button title="删除" v-if="btns.delete.enable"
                        class="ui circular icon primary button tiny"
                        @click.stop.prevent="batch('delCom','batch')">
                    <i class="icon remove"></i>
                </button>
                <op_dir :btns="btns"></op_dir>
            </div>
            <div class="eight wide column">

                <button title="新增" v-if="btns.add.enable"
                        class="ui circular icon primary button tiny"
                        @click="addCom()">
                    <i class="icon plus"></i>
                </button>

                <button title="搜索" v-if="btns.search.enable" class="ui circular icon primary button tiny"
                        @click="showModal"><i class="icon search"></i></button>

                <button title="导出" v-if="btns.export.enable" class="ui circular icon primary button tiny"
                        @click="toExport"><i
                    class="icon download"></i></button>

                <button title="刷新数据" class="ui circular icon primary button tiny" @click="refresh"><i
                    class="icon refresh"></i>
                </button>


                <listTop v-if="is_admin" @showModal='showModal("menu_group")'></listTop>

                <button v-if="$store.state.current_ctl!='Taskcom'" class="ui primary button tiny" @click="clearSearch">
                    清除搜索
                </button>
                <button class="ui button tiny" @click="clearCache">清除缓存</button>

                <button title="隐藏列" class="ui button tiny" @click="toctlFields">隐藏列
                </button>
                <!--<button title="隐藏列" class="ui button tiny" @click="topage">topage-->
                <!--</button>-->


            </div>
        </div>

    </div>
</template>
<script>
    import datepicker from 'components/common/datepicker'
    import listTop from 'components/ctls/listTop'
    import op_dir from 'components/list/op/index';
    import util from '_util/index';
    import logic from '_logic';
    import tool from 'src/util/tool'
    import model from '_model'

    export default {
        data() {
            return {
                dateAll: {
                    startTime: {
                        time: ''
                    },
                    endTime: {
                        time: ''
                    }
                },
                titleShow: true,
                authLoaded: false,
                btns: {
                    add: {},
                    export: {},
                    onOff: {},
                    delete: {},
                    timeSearch: {},
                    search: {}
                },
                auths: null,
                showadd: 0,
                showsearch: 0,
                userName: this.$cookie.get('txy_name'),
                downloading: false,
                showexport: 0
            }
        },
        methods: {
//            topage() {
//                this.$router.push('/page/list?ctl=TopProduct&to_p=10');
//            },
            setAuth() {
                this.auths = util.modelFn('Auth', 'get_type_auth', this, 'op');
                this.setBtnsConf();
                this.authLoaded=true;
            },
            batch(event, flag = null) {
                if (this.$parent.selected.length <= 0)
                    util.showMsg(this, 'quick_error', '请选择数据');
                else {
                    let confirm = flag == -1 ? logic.list.get_del_msg(this) : '确认此操作吗？';
                    util.showConfirm(this, () => {
                        let eventVue = util.store.get(this,'eventVue');
                        eventVue.$emit(event, flag)
                    }, {content: confirm});
                }
            },
            toctlFields() {
                let _vo = {
                    list_fileds: this.list_fileds
                };
                logic.edit.showEditPop(this, 73, () => {

                    let vo = util.store.get(this, 'editVoModal');

                    let show_fields = vo.show_fields ? vo.show_fields.split(',') : '';
                    this.$emit('toctlFields', show_fields);


                }, _vo);
            },

            download(){
                this.$store.dispatch('download', {}).then((msg) => {
                    this.$set(this.$data, 'downloading', false)

                })
                this.$set(this.$data, 'downloading', true)

            },

            setBtnsConf() {
                for (let key in this.btns) {
                    this.btns[key].enable = false;
                }

                let btns = this.modelConf.btns;
                if (btns) {
                    JSON.parse(btns).forEach(btn => {
                        let b = this.auths ? this.auths.contains(btn.code) : true;
                        if(this.is_admin||b)
                            this.btns[btn.code].enable = true
                    })
                }
            },
            clearSearch() {
                let eventVue = util.store.get(this,'eventVue');
                eventVue.$emit('clearDate')
                eventVue.$emit('clearSearchVo')
                eventVue.$emit('clearModalSearchField')
            },
            clearSearch_notsearch() {
                let eventVue = util.store.get(this,'eventVue');
                eventVue.$emit('clearDate')
                eventVue.$emit('clearSearchVo_notsearch')
                eventVue.$emit('clearModalSearchField')
            },
            clearCache() {
                let url = tool.getApi(this, 'Post', 'clearCache');
                util.http.SR(this, url, {model: this.ctl}, '清除缓存成功!', () => {
                    this.$emit('refresh',1);
                });
            },
            showModal(params, event) {
                event = event || 'showModal'
                params = params || 'search'

                this.$emit(event, params)
            },
            doDateSearch() {

                this.$emit('doSearch')
            },
            setSearchDate(val) {
                this.$emit('setSearchDate', val)
            },
            refresh() {
                this.$emit('refresh')
            },
            toExport() {
                util.showConfirm(this, () => {
                    this.$emit('toExport')
                }, {content: '确认导出吗？'});
            },
            beforeAdd(clb) {
                let _model = model[this.ctl];
                if (_model && _model.beforeAdd) {
                    _model.beforeAdd(this, clb);
                } else {
                    clb();
                }

            },
            addCom() {
                let is_modal_edit=logic.edit.is_modal_edit(this)
                if (!is_modal_edit) {
                    this.beforeAdd(() => {
                        util.jump(this, 1)
                    })
                } else {
                    util.store.commit(this,'setOp', 'insert');
                    util.store.commit(this,'setEditVo', {});
                    util.eventEmit(this, 'showModalEdit');
                }
            },
            hlAuth(){
                this.$store.dispatch('module_permission/initOperate').then((msg) => {
                    let auth = this.$store.state.global.tabledata[this.ctl];
                    if (auth) {
                        this.$set(this, 'showadd', auth.ADD)
                        this.$set(this, 'showsearch', auth.SEARCH)
                        this.$set(this, 'showexport', auth.EXPORT)
                    }
                });
                this.authLoaded = true;
            }
        },
        components: {
            datepicker,
            op_dir,
            listTop
        },

        mounted(){
        },
        watch: {
            modelConf: function () {
                this.setBtnsConf()
            },
            ctl: function (val, old) {
                if (val != old) {
                    this.setAuth();
                }
            }
        },

        computed: {
            ctl() {
                return this.$store.state.global.current_ctl
            },
            need_del(){
                return logic.list.get_need_del(this);
            },
            is_admin(){
                return util.is_admin(this);
            },
        }
        ,
        created(){
//            this.hlAuth();
            this.setAuth();


            let eventVue = util.store.get(this,'eventVue');
            eventVue.$on('clearListSearch', this.clearSearch_notsearch)
        },
        props: ['modelConf', 'list_fileds']

    }
</script>
<style rel="stylesheet/less" lang="less">@dir: "common/";

.title-wrapper {
    position: relative;
    .search {
    }

}
</style>
